package com.example.toolbar.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.example.toolbar.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class CityWideFragment extends Fragment {
	@ViewInject(R.id.viewpager)
	private ViewPager viewPager;
	@ViewInject(R.id.line)
	private TextView line;
	private TextView[] labels = new TextView[4];
	private Activity activity;

	private int prePos; // 线条上一次的位置(动画的起始位置)
	private int lineLength; // 线条的长度
	private Fragment[] fragments = { new AllAcitonFragment(),
			new AllAcitonFragment(), new AllAcitonFragment(),
			new AllAcitonFragment() };

	private int getScreenWidth() {
		DisplayMetrics dm = new DisplayMetrics();
	    activity.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		activity = getActivity();
		View root = inflater.inflate(R.layout.city_wide_fragment_viewpager,
				null);
		labels[0] = (TextView) root.findViewById(R.id.qb);
		labels[1] = (TextView) root.findViewById(R.id.yks);
		labels[2] = (TextView) root.findViewById(R.id.wks);
		labels[3] = (TextView) root.findViewById(R.id.yjs);
		ViewUtils.inject(this, root);
		viewPager.setOnPageChangeListener(new OnPageChangeAdapter());
		viewPager.setAdapter(new CityWideAdapter(getChildFragmentManager()));
		lineLength = getScreenWidth() / 4;
		addListener();
		return root;
	}

	class CityWideAdapter extends FragmentPagerAdapter {
		public CityWideAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return fragments[arg0];
		}

		@Override
		public int getCount() {
			return fragments.length;
		}

	}
	 private void addListener() {
         for (int i = 0; i < labels.length; i++) {
              labels[i].setOnClickListener(new OnChangePageListener(i));
         }
    }


	class OnPageChangeAdapter implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			for (int i = 0; i < labels.length; i++) {
				labels[i].setTextColor(Color.BLACK);
			}
			labels[position].setTextColor(Color.BLUE);

			// 当前位置(动画的结束位置)
			int curPos = position * lineLength;

			TranslateAnimation ani = new TranslateAnimation(prePos, curPos, 0,
					0);
			ani.setDuration(200);
			ani.setFillAfter(true);
			ani.setInterpolator(new AccelerateInterpolator());

			line.startAnimation(ani);

			prePos = curPos;
		}

	}
	class OnChangePageListener implements View.OnClickListener {
        private int position;

        public OnChangePageListener(int position) {
             super();
             this.position = position;
        }

        @Override
        public void onClick(View v) {
             viewPager.setCurrentItem(position);

        }
   }

}
